package com.example.jdk8.threads;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

public class ThreadInvokeAny {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 初始化线程池
        ExecutorService executorService = Executors.newWorkStealingPool();
        // 自定义方法创建线程
        List<Callable<String>> callableList = Arrays.asList(
                callable("task1", 2),
                callable("task2", 1),
                callable("task3", 3));
        String s = executorService.invokeAny(callableList);
        System.out.println(s); //task2-找执行时间最短的那个线程输出

    }

    private static Callable<String> callable(String result, int i) {
        return ()->{
            TimeUnit.SECONDS.sleep(i);
            return result;
        };
    }

}
